<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="销售单">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();


            ajaxSelects("${baseUrl}user/user/getAll.action.action",[
                {selectName:"userId",head:"全部" },
                {selectName:"makeUserId",head:"全部" }]);


            ajaxSelect("${baseUrl}organization/store/getStores.action","storeId","全部");

            ajaxSelect("${baseUrl}settAccount/settAccount/listVo.action","settAccountId","全部");
            ajaxSelect("${baseUrl}organization/house/listNormalVo.action","houseId","全部");
        });

        layui.use(['form', 'table','laydate'], function () {
            let $ = layui.jquery,
                form = layui.form,
                laydate = layui.laydate,
                table = layui.table,
                util = layui.util;
            laydate.render({
                elem: '#date',
                range:'~',
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="date1"]').val(getDateFromLayUiDate(date));
                        $('[name="date2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="date1"]').val('');
                        $('[name="date2"]').val('');
                    }
                }
            });

            laydate.render({
                elem: '#makeTime',
                range:'~',
                done: function(value, date, endDate){
                    if (!emptyEntity(value)) {
                        $('[name="makeTime1"]').val(getDateFromLayUiDate(date));
                        $('[name="makeTime2"]').val(getDateFromLayUiDate(endDate));
                    } else {
                        $('[name="makeTime1"]').val('');
                        $('[name="makeTime2"]').val('');
                    }
                }
            });

            renderTable();

            table.on('tool(table)', function (obj) {
                let data = obj.data;
                switch(obj.event){
                    case 'ticket':
                        ticket(data);
                        break;
                    case 'backTicket':
                        backTicket(data);
                        break;
                }
            });

            table.on('toolbar(table)', function (obj) {
                let data = table.checkStatus('table').data;
                switch(obj.event){
                    case 'batchTicket':
                        batchTicket(data);
                        break;
                };
            });

            form.on('checkbox(noHas)', function (data) {
                table.reload('table',{
                    where: genSearch()
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            });

            form.on('submit(search)', function (data) {
                table.reload('table',{
                    where: genSearch()
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });

        });

        function clearSearch() {
            $("#searchForm")[0].reset();
            $('[name="date1"]').val('');
            $('[name="date2"]').val('');
            $('[name="makeTime1"]').val('');
            $('[name="makeTime2"]').val('');
        }


        function renderTable() {
            clearSearch();
            layui.table.render({
                id:'table',
                elem: '#table',
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                url: "${baseUrl}funds/saleTicket/getList.action",
                totalRow:true,
                where : {
                    ticket :2,
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {type:'checkbox', fixed:'left'},
                    {width:70,title:"序号",templet:function (d) {
                        return d.LAY_INDEX;
                    }},
                    {width: 100, title: '业务日期',style:"font-size:12px;",templet: function (d) {
                            return GMTToDate(d.date);
                        }},
                    {width: 100, title: '经手人',style:"font-size:12px;",templet: function (d) {
                            return d.user == null ? "" :d.user.name;
                        }},
                    {field: 'no', width: 180, title: '单据编号',style:"font-size:12px;", templet: '#noBar'},


                    {width: 120, title: '发票状态',style:"font-size:12px;", templet: function (d) {
                            return d.ticket == 2 ? "未开票" : "已开票";
                        }},
                    {width: 120, title: '客户',style:"font-size:12px;", templet: function (d) {
                            return d.custom == null ? "" :d.custom.name;
                        }},
                    {width: 120, title: '欠款金额',style:"font-size:12px;",templet: function (d) {
                            if (d.realAmt < d.totalAmt){
                                return getPrice(d.totalAmt - d.realAmt);
                            } else {
                                return '-';
                            }
                        }},
                    {title: '操作', minWidth: 80,fixed:'right', templet: '#operBar'},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    $(".saleDetail").click(function () {
                        openTab("销售单详情","${baseUrl}sale/saleDetail.action?id="+$(this).attr("id"));
                    });
                }
            });
        }



        function genSearch() {
            let json = getFormJson($("#searchForm"));
            json.ticket = $('[name="noHas"]').next().hasClass("layui-form-checked") ? 2:'';
            return json;
        }

        function ticket(data) {
            tipLoad("开票中...");
            post("${baseUrl}funds/saleTicket/ticket.action",{id:data.id}, function (res) {
                closeLoading();
                if (res.code == 1) {
                    layui.table.reload('table',{
                        where: genSearch()
                        ,page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                } else {
                    errTip(res.msg);
                }
            });
        }

        function backTicket(data) {
            tipLoad("撤销开票中...");
            post("${baseUrl}funds/saleTicket/backTicket.action",{id:data.id}, function (res) {
                closeLoading();
                if (res.code == 1) {
                    layui.table.reload('table',{
                        where: genSearch()
                        ,page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                } else {
                    errTip(res.msg);
                }
            });
        }

        function batchTicket(data) {
            if (!checkSelectNum(data)){
                return;
            }

            if (hasSelect(data,[{key:"ticket",value:3,tip:"开票"}])){
                return;
            }

            tipLoad("批量开票中...");
            post("${baseUrl}funds/saleTicket/batchTicket.action",genIdList(data), function (res) {
                closeLoading();
                if (res.code == 1) {
                    layui.table.reload('table',{
                        where: genSearch()
                        ,page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                } else {
                    errTip(res.msg);
                }
            });
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="" id="searchForm">
            <input type="hidden" name="date1">
            <input type="hidden" name="date2">
            <input type="hidden" name="makeTime1">
            <input type="hidden" name="makeTime2">

            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="checkbox" name="noHas" title="不显示已开票" lay-skin="primary" value="true" checked lay-filter="noHas">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">搜索信息</label>
                    <div class="layui-input-inline">
                        <input type="text" name="search" style="font-size: 12px;" placeholder="编号、客户" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <a class="layui-btn" lay-submit="" lay-filter="search"><i class="layui-icon layui-icon-search"></i></a>
                    <a class="layui-btn " id="selectSearch">筛选</a>
                </div>

                <div class="table-tool-div-screen" status="close" style="display: none">
                    <div class="layui-inline">
                        <label class="layui-form-label">单据编号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="no" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">商品名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="goodsName" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-inline">
                            <input type="text" name="remark" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">客户名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="custom" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">门店</label>
                        <div class="layui-input-inline">
                            <select name="storeId"></select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">结算账户</label>
                        <div class="layui-input-inline">
                            <select name="settAccountId"></select>
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">业务日期</label>
                        <div class="layui-input-inline">
                            <input type="text" id="date" placeholder="请选择范围" class="layui-input" autocomplete="off">
                        </div>
                    </div>


                    <div class="layui-inline" >
                        <label class="layui-form-label">经手人</label>
                        <div class="layui-input-inline">
                            <select name="userId"></select>
                        </div>
                    </div>


                    <div class="layui-inline">
                        <label class="layui-form-label">制单日期</label>
                        <div class="layui-input-inline">
                            <input type="text" id="makeTime" placeholder="请选择范围" class="layui-input" autocomplete="off">
                        </div>
                    </div>

                    <div class="layui-inline" >
                        <label class="layui-form-label">制单人</label>
                        <div class="layui-input-inline">
                            <select name="makeUserId"></select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <a class="layui-btn" lay-submit="" lay-filter="search">搜索</a>
                        <a class="layui-btn layui-btn-primary" id="clearSearch">清空所有条件</a>
                    </div>
                </div>
                <script>
                    $("#selectSearch").click(function () {
                        if ($(".table-tool-div-screen").attr("status")=="close") {
                            $(".table-tool-div-screen").css("display","block");
                            $(".table-tool-div-screen").attr("status","open");
                            $("#search").parent().parent().css("display","none");
                            $("#search").val("");
                            $("#btn-search").css("display","none");
                            $(this).html("收起");
                        } else {
                            $(".table-tool-div-screen").css("display","none");
                            $(".table-tool-div-screen").attr("status","close");
                            $("#search").parent().parent().css("display","inline-block");
                            $("#btn-search").css("display","inline-block");
                            $(this).html("筛选");
                        }
                    });

                    $("#clearSearch").click(function () {
                        clearSearch();
                    });
                </script>

            </div>
        </form>
        <hr/>

        <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>
    </div>
</div>
</body>

<script type="text/html" id="tableToolBar">
    <div class="layui-inline">
        <a class="layui-btn" lay-event="batchTicket">批量开票</a>
    </div>
</script>

<script type="text/html" id="operBar">
    {{# if(d.ticket==2){ }}
    <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="ticket">开票</a>
    {{# }else{ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger table-operator" lay-event="backTicket">撤销开票</a>
    {{# } }}
</script>

<script type="text/html" id="noBar">
    <a href="#" class="saleDetail" id="{{d.id}}">
        {{d.no}}

        {{# if(d.retail){ }}
        <span class='layui-badge layui-bg-blue' style='margin-left: 4px;'>零售</span>
        {{# } }}
    </a>
</script>

<html:loading/>
</html>